#ifndef CBUSSCHEDULE_H
#define CBUSSCHEDULE_H

#include "QTime"
#include <QString>
#include <QHash>
#include "cbusschedulerxmlreader.h"
#include "QSet"

class CBusScheduler
{
public:
    static void InitInstance();
    static CBusScheduler* GetInstance();
    static void Destroy();

private:
    CBusScheduler(){}
    ~CBusScheduler(){}

public:
    // read bus info from xml file and generate a hash table
    bool ReadBusInfoFromXML(QString filename);

    // according to the startPlace, endPlace and expectedTime, the system will
    // pick up qualified schedules that match.
    void GetBusArrivingTime(QString startPlace, QString endPlace, QTime expectedTime,\
                            vector<BUS_INFO_FOR_OUT>& result);

    // just pick the bus station info
    int GetSpecifiedBusInfo(QString busName, QTime expectedTime, vector<STATION_INFO_FOR_OUT>& result);

    // get all the general info of buses
    void GetBusGeneralInfo(QList< vector<BUS_INFO_NAME_BASED> >& result)
    {
        result.clear();
        result = (m_xmlReader->GetNameBasedResult()).values();
    }

    // get all the available station names, unique and sort by name
    void GetAllStationNames(QSet<QString>& result)
    {
        result.clear();
        QList<QString> temp = (m_xmlReader->GetStationBasedResult()).keys();
        qSort(temp);
        result = temp.toSet();
    }

private:
    void FindOutQualifiedSchedulers(const BUS_INFO_STATION_BASED* pStartBus, \
                                    const BUS_INFO_STATION_BASED* pEndBus, \
                                    const QTime expectedTime, \
                                    BUS_INFO_FOR_OUT& partialResult);


public:
    // for test purpose only
    void TestBusInfoMap();

    // for test purpose only
    void TestGetSpecidfiedBusInfoResult(vector<STATION_INFO_FOR_OUT>& result);

    // for test purpose only
    void TestGetBusArrivingTimeResult(vector<BUS_INFO_FOR_OUT>& result);

private:
    static CBusScheduler * m_busSchedule;
    static CBusSchedulerXMLReader *m_xmlReader;

    // the key is station name, while the data is bus station information
    QHash<QString, BUS_INFO_STATION_BASED> m_stationInfoHash;

    // the pair is busName, and a list of station info
    QMap<QString, vector<BUS_INFO_NAME_BASED> > m_busInfoMap;
};

#endif // CBUSSCHEDULE_H
